package com.lite.haru.alarmpolice.activity;

import org.kymjs.kjframe.ui.KJFragment;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.balysv.materialmenu.MaterialMenuDrawable;
import com.balysv.materialmenu.MaterialMenuIcon;
import com.balysv.materialmenu.MaterialMenuDrawable.Stroke;
import com.lite.haru.R;
import com.lite.haru.alarmpolice.fragment.AlarmPendingFrag;
import com.lite.haru.alarmpolice.fragment.AlarmProcessedFrag;
import com.lite.haru.constant.LocationService;
import com.lite.haru.constant.PoliceApplication;
import com.lite.haru.myadapter.RightLVAdapter;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AlarmMainAty extends FragmentActivity {

	private LocationService locationService;
	int menu_flag = 0;
	private AlarmPendingFrag pendfra;
	private AlarmProcessedFrag profra;

	/** DrawerLayout */
	private DrawerLayout mDrawerLayout;
	/** 左边栏菜单 */
	private ListView mMenuListView;
	/** 右边栏菜单 */
	private ListView mRightListView;
	/** 左菜单列表 */
	private String[] mMenuTitles;
	/** 右菜单列表 */
	private String[] mRightTitles;

	private RightLVAdapter adapter;
	/** Material Design风格 */
	private MaterialMenuIcon mMaterialMenuIcon;
	/** 菜单打开/关闭状态 */
	private boolean isDirection_left = false;
	/** 右边栏打开/关闭状态 */
	private boolean isDirection_right = false;
	private View showView;
	public String co, pr, ci;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_main);
		ActionBar actionBar = getActionBar();

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mMenuListView = (ListView) findViewById(R.id.left_drawer);
		mRightListView = (ListView) findViewById(R.id.right_drawer);
		this.showView = mMenuListView;

		// 初始化菜单列表
		mMenuTitles = new String[] { "待处理信息", "已处理信息" };
		mRightTitles = new String[] { "设置", "退出应用" };

		mMenuListView.setAdapter(new ArrayAdapter<String>(this,
				R.layout.item_drawer_left_list, mMenuTitles));

		mMenuListView.setOnItemClickListener(new DrawerItemClickListener());

		// 设置抽屉打开时，主要内容区被自定义阴影覆盖
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);
		// 设置ActionBar可见，并且切换菜单和内容视图
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);

		mMaterialMenuIcon = new MaterialMenuIcon(this, Color.WHITE, Stroke.THIN);// 设置菜单的颜色和粗细
		mDrawerLayout.setDrawerListener(new DrawerLayoutStateListener());

		if (savedInstanceState == null) {
			selectLeftItem(0);
		}
		initRight();

	}

	public void initRight() {
		adapter = new RightLVAdapter(mRightTitles, getApplicationContext());
		mRightListView.setAdapter(adapter);

		mRightListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				selectRightItem(position);
			}
		});
	}

	/**
	 * 加载菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * ListView上的Item点击事件
	 * 
	 */
	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			selectLeftItem(position);
			menu_flag = position;
		}
	}

	/**
	 * 切换主视图区域的Fragment
	 * 
	 * @param position
	 */
	private void selectLeftItem(int position) {

		switch (position) {
		case 0:
			if (pendfra == null) {
				pendfra = new AlarmPendingFrag();
				Bundle b = new Bundle();
				pendfra.setArguments(b);
			}
			changeFragment(R.id.content_frame, pendfra);

			break;
		case 1:
			if (profra == null) {
				profra = new AlarmProcessedFrag();
				Bundle b = new Bundle();
				profra.setArguments(b);
			}

			changeFragment(R.id.content_frame, profra);
			break;

		default:
			break;
		}

		// 更新选择后的item和title，然后关闭菜单
		mMenuListView.setItemChecked(position, true);
		setTitle(mMenuTitles[position]);
		mDrawerLayout.closeDrawer(mMenuListView);
	}

	private void selectRightItem(int position) {

		switch (position) {
		case 0:
			mDrawerLayout.closeDrawer(mRightListView);
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), AlarmSettingAty.class);
			startActivity(intent);
			break;

		case 1:
			System.exit(0);// 退出应用
			break;

		default:
			break;
		}

	}

	/**
	 * DrawerLayout状态变化监听
	 */
	private class DrawerLayoutStateListener extends
			DrawerLayout.SimpleDrawerListener {
		/**
		 * 当导航菜单滑动的时候被执行
		 */
		@Override
		public void onDrawerSlide(View drawerView, float slideOffset) {
			showView = drawerView;
			if (drawerView == mMenuListView) {// 根据isDirection_left决定执行动画
				mMaterialMenuIcon.setTransformationOffset(
						MaterialMenuDrawable.AnimationState.BURGER_ARROW,
						isDirection_left ? 2 - slideOffset : slideOffset);
			} else if (drawerView == mRightListView) {// 根据isDirection_right决定执行动画
				mMaterialMenuIcon.setTransformationOffset(
						MaterialMenuDrawable.AnimationState.BURGER_ARROW,
						isDirection_right ? 2 - slideOffset : slideOffset);
			}
		}

		/**
		 * 当导航菜单打开时执行
		 */
		@Override
		public void onDrawerOpened(android.view.View drawerView) {
			if (drawerView == mMenuListView) {
				isDirection_left = true;

			} else if (drawerView == mRightListView) {
				isDirection_right = true;
				setTitle("个人中心");
			}
		}

		/**
		 * 当导航菜单关闭时执行
		 */
		@Override
		public void onDrawerClosed(android.view.View drawerView) {
			if (drawerView == mMenuListView) {
				isDirection_left = false;
			} else if (drawerView == mRightListView) {
				isDirection_right = false;
				showView = mMenuListView;
				setTitle(mMenuTitles[menu_flag]);
			}
		}
	}

	/**
	 * 点击ActionBar上菜单
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			if (showView == mMenuListView) {
				if (!isDirection_left) { // 左边栏菜单关闭时，打开
					mDrawerLayout.openDrawer(mMenuListView);
				} else {// 左边栏菜单打开时，关闭
					mDrawerLayout.closeDrawer(mMenuListView);
				}
			} else if (showView == mRightListView) {
				if (!isDirection_right) {// 右边栏关闭时，打开
					mDrawerLayout.openDrawer(mRightListView);
				} else {// 右边栏打开时，关闭
					mDrawerLayout.closeDrawer(mRightListView);
				}
			}
			break;
		case R.id.action_personal:
			if (!isDirection_right) {// 右边栏关闭时，打开
				if (showView == mMenuListView) {
					mDrawerLayout.closeDrawer(mMenuListView);
				}
				mDrawerLayout.openDrawer(mRightListView);
				setTitle("个人中心");

			} else {// 右边栏打开时，关闭
				mDrawerLayout.closeDrawer(mRightListView);
				setTitle(mMenuTitles[menu_flag]);
			}

			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 根据onPostCreate回调的状态，还原对应的icon state
	 */
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mMaterialMenuIcon.syncState(savedInstanceState);
	}

	/**
	 * 根据onSaveInstanceState回调的状态，保存当前icon state
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		mMaterialMenuIcon.onSaveInstanceState(outState);
		super.onSaveInstanceState(outState);
	}

	/**
	 * 用Fragment替换视图
	 * 
	 * @param resView
	 *            将要被替换掉的视图
	 * @param targetFragment
	 *            用来替换的Fragment
	 */
	public void changeFragment(int resView, KJFragment targetFragment) {
		FragmentTransaction transaction = getFragmentManager()
				.beginTransaction();
		transaction.replace(resView, targetFragment, targetFragment.getClass()
				.getName());
		transaction.commit();
	}

	protected void onStart() {
		super.onStart();
		// -----------location config ------------
		locationService = ((PoliceApplication) getApplication()).locationService;
		// 获取locationservice实例，建议应用中只初始化1个location实例，然后使用，可以参考其他示例的activity，都是通过此种方式获取locationservice实例的
		locationService.registerListener(mListener);
		// 注册监听
		int type = getIntent().getIntExtra("from", 0);
		if (type == 0) {
			locationService.setLocationOption(locationService
					.getDefaultLocationClientOption());
		} else if (type == 1) {
			locationService.setLocationOption(locationService.getOption());
		}

		locationService.start();// 定位SDK
	}

	/***
	 * Stop location service
	 */
	protected void onStop() {

		// TODO Auto-generated method stub
		locationService.unregisterListener(mListener); // 注销掉监听
		locationService.stop(); // 停止定位服务
		super.onStop();
	}

	private BDLocationListener mListener = new BDLocationListener() {

		@Override
		public void onReceiveLocation(BDLocation location) {
			co = location.getCountry();
			ci = location.getCity();
			pr = location.getProvince();
		}

	};

}